<template>
  <a-modal
    style="top: 20px"
    :title="title"
    :width="800"
    :ok="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="close"
    @ok="add"
    cancelText="关闭"
  >
    <depart-form :treeData="treeData" :formData="formData" :isAdd="true" ref="departForm"></depart-form>
  </a-modal>
</template>

<script>
import DepartForm from './DepartForm'
import { apiAddDepart } from '@/api/system'

export default {
  name: 'DepartModal',
  components: {
    DepartForm
  },
  props: {
    // 部门树
    treeData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      title: '',
      visible: false,
      confirmLoading: false,
      formData: {}
    }
  },
  watch: {
    formData (val) {
      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.departForm.setFormValue(val)
        })
      })
    }
  },
  methods: {
    show (title, record) {
      this.title = title
      this.visible = true
      this.formData = record
      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.departForm.form.resetFields()
        })
      })
    },
    close () {
      this.visible = false
    },
    add () {
      this.$refs.departForm.form.validateFields((err, values) => {
        if (!err) {
          apiAddDepart(values).then(res => {
            console.log(res)
            if (res.success) {
              this.$message.success('保存成功!')
              this.$emit('ok')
              this.close()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
